package com.sto.stoapp.file;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;

import org.apache.http.util.EncodingUtils;

import android.os.Environment;

/**
 * 存储订单提醒内容
 * @author wxf
 *
 */
public class OrderNoticeTempFile {
	private String OrderFileName= "OrderFileName.txt";
	File sdcardFile = Environment.getExternalStorageDirectory();
	
	public String readFile(){
		String contents="";
		
		File file = new File(sdcardFile.toString() + File.separator + OrderFileName);
		
		try {
			FileInputStream fis = new FileInputStream(file);    
			int length = fis.available();
			
			byte [] buffer = new byte[length];   
	        fis.read(buffer);
	        
	        contents = EncodingUtils.getString(buffer, "utf-8");   
	        
	        fis.close();       

			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			
		}
		
		return contents;
	}
	
	
	public boolean getHasFile(){
		File file = new File(sdcardFile.toString() + File.separator + OrderFileName);
		
		return file.exists();
	}
	
	/**
	 * 插入订单提示信息
	 * @param contents
	 */
	public void insertContents(String contents){
		File sdcardFile = Environment.getExternalStorageDirectory();
		File file = new File(sdcardFile.toString() + File.separator + OrderFileName);
		FileWriter fw = null; 

        BufferedWriter bw = null; 
		if(!file.exists()){
			try {
				file.createNewFile();
				
				
				fw = new FileWriter(file, true);
				bw = new BufferedWriter(fw);

				bw.write(contents);

				bw.flush(); // 刷新该流的缓冲 

	            bw.close(); 

	            fw.close(); 
				
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				 try { 

		                bw.close(); 

		                fw.close(); 

		            } catch (IOException e1) { 

		                // TODO Auto-generated catch block 

		            }
			}
		}
	}
	
	/**
	 * 删除内容
	 *@author wxf
	 * 
	 */
	public void deleteContents(){
		//File sdcardFile = Environment.getExternalStorageDirectory();
		File file = new File(sdcardFile.toString() + File.separator + OrderFileName);
		if(file.exists()){
			file.delete();		
		}
	}
}
